from .base_handler import BaseRequestHandler
import json


class ConfigManageHandler(BaseRequestHandler):
    """配置数据"""
    async def get_default(self):
        self.success_data = {
            'keyword': {
                'web_ui': await self.mdb.query_by_dict(table='test_keyword', where={'word_type': 1}, order_by='id'),
                'function': await self.mdb.query_by_dict(table='test_keyword', where={'word_type': 8}, order_by='id'),
                'android': await self.mdb.query_by_dict(table='test_keyword', where={'word_type': 2}, order_by='id'),
                'ios': await self.mdb.query_by_dict(table='test_keyword', where={'word_type': 2}, order_by='id'),
                'interface': await self.mdb.query_by_dict(table='test_keyword', where={'word_type': 3}, order_by='id'),
                'database': await self.mdb.query_by_dict(table='test_keyword', where={'word_type': 4}, order_by='id'),
                'windows_ui': await self.mdb.query_by_dict(table='test_keyword', where={'word_type': 5}, order_by='id')
            },
            'element_type': ['web', 'web_frame', 'android', 'ios'],
            'step_type': ['web_ui', 'android', 'ios', 'interface', 'database', 'snippet', 'function', 'code'],
            'case_type': ['base', 'setup', 'main', 'sub', 'skip', 'snippet'],
            'location_by': await self.mdb.query_by_dict(table='test_keyword', where={'word_type': 11}, order_by='id'),
            'step_control': await self.mdb.query_by_dict(table='test_keyword', where={'word_type': 12}),
            'test_config': dict([(line['name'], line) for line in await self.mdb.query_by_dict(table='test_config')])
        }


class KeywordManageHandler(BaseRequestHandler):
    """关键字管理"""
    async def get_default(self, *args, **kwargs):
        """查询现有关键字信息"""
        self.success_data = await self.mdb.query("select * from test_keyword", fields_s2json=('params',))

    async def post_default(self, *args, **kwargs):
        """增加关键字"""
        d = {
            'en_name': self.escape_string(self.request_json['en_name']),
            'cn_name': self.escape_string(self.request_json['cn_name']),
            'word_type': self.request_json['word_type'],
            'params': self.request_json['params'],
            'explain': self.request_json['explain'],
            'mark': self.request_json.get('mark')
        }
        await self.mdb.insert_by_dict(table='test_keyword', data=d)

    async def patch_default(self, *args, **kwargs):
        """修改关键字"""
        kw_id = self.request_json['id']
        d = {
            'en_name': self.escape_string(self.request_json.get('en_name')),
            'cn_name': self.escape_string(self.request_json.get('cn_name')),
            'word_type': self.request_json.get('word_type'),
            'params': self.request_json.get('params'),
            'explain': self.request_json.get('explain'),
            'mark': self.request_json.get('mark')
        }
        await self.mdb.update_by_dict(table='test_keyword', colv=self.data_tool.clean_dict_none(d), where={'id': kw_id})

